package com.fruitshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fruitshop.entity.Category;
import com.fruitshop.exception.BusinessException;
import com.fruitshop.mapper.CategoryMapper;
import com.fruitshop.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Comparator;

/**
 * 分类服务实现
 */
@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    
    /**
     * 获取分类列表
     *
     * @return 分类列表
     */
    @Override
    public List<Category> listCategories() {
        return list(new LambdaQueryWrapper<Category>()
                .orderByAsc(Category::getSortOrder)
                .orderByDesc(Category::getId));
    }
    
    /**
     * 获取分类详情
     *
     * @param id 分类ID
     * @return 分类详情
     */
    @Override
    public Category getCategoryById(Long id) {
        Category category = getById(id);
        if (category == null) {
            throw new BusinessException("分类不存在");
        }
        return category;
    }
} 